﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Windows;
using System.Windows.Ink;
using System.IO;
using System.Windows.Controls;

namespace Vtron.HipcConfigurationTools
{
    class DataStore
    {
        /// <summary>
        /// Gets or Sets the Window1 instance used in this application
        /// </summary>
        static public MainWindow ParentWindow
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or Sets the path to the folder.xml file in the IntelligentWall Directory
        /// </summary>
        static public string CodecXML
        {
            get;
            set;
        }

        ///// <summary>
        ///// Saves the location of the various DraggableButtons to the user's folder
        ///// </summary>
        ///// <param name="path">The path of the folder images were taken from</param>
        ///// <param name="buttonList">List of the buttons currently displayed on the canvas</param>
        //static public void SaveImageInformation(string path, List<DraggableButton> buttonList)
        //{
        //    XmlDocument xmlDoc = new XmlDocument();
        //    XmlElement imagesElement = xmlDoc.CreateElement("NoteTable");

        //    foreach (DraggableButton db in buttonList)
        //    {
        //        XmlElement imageElement = xmlDoc.CreateElement("Image");

        //        XmlAttribute xAttribute = xmlDoc.CreateAttribute("X");
        //        imageElement.SetAttributeNode(xAttribute);
        //        xAttribute.Value = db.xPosition.ToString();

        //        XmlAttribute yAttribute = xmlDoc.CreateAttribute("Y");
        //        imageElement.SetAttributeNode(yAttribute);
        //        yAttribute.Value = db.yPosition.ToString();

        //        imageElement.InnerText = db.ImagePath;
        //        imagesElement.AppendChild(imageElement);
        //    }

        //    xmlDoc.AppendChild(imagesElement);
        //    xmlDoc.Save(path);
        //}

        //    /// <summary>
        //    /// Helps keep track of current images and their positions
        //    /// </summary>
        //    /// <param name="path">The path to the XML file corresponding to a folder's images</param>
        //    /// <returns>A dictionary object storing the path and positions of each image</returns>
        //    static public Dictionary<string, Point> GetXMLData(string path)
        //    {
        //        Dictionary<string, Point> imagePositionPath = new Dictionary<string, Point>();

        //        XmlDocument doc = new XmlDocument();
        //        doc.Load(path);

        //        XmlNodeList xmlImageList = doc.GetElementsByTagName("Image");

        //        foreach (XmlNode node in xmlImageList)
        //        {
        //            XmlElement imageElement = (XmlElement)node;

        //            string imagePath = imageElement.InnerText;
        //            string xPos = "";
        //            string yPos = "";

        //            if (imageElement.HasAttributes)
        //            {
        //                xPos = imageElement.Attributes["X"].InnerText;
        //                yPos = imageElement.Attributes["Y"].InnerText;
        //            }

        //            imagePositionPath.Add(imagePath, new Point(Convert.ToDouble(xPos), Convert.ToDouble(yPos)));
        //        }

        //        return imagePositionPath;
        //    }

        //    /// <summary>
        //    /// Returns the StrokeCollection from the .INK file stored in the user's folder
        //    /// </summary>
        //    /// <param name="path">The path to the .INK file storing the Ink data</param>
        //    /// <returns>A collection of strokes</returns>
        //    public static StrokeCollection GetInkDisplay(string path)
        //    {
        //        string ImageContent = path;
        //        string UserFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\NoteTable";

        //        InkPresenter ip = new InkPresenter();

        //        string fileName = FileFolderChecker.ReturnPathAsName(UserFolder) + System.IO.Path.GetFileNameWithoutExtension(ImageContent);
        //        string filePath = UserFolder + @"\\" + fileName + ".Ink";

        //        if (File.Exists(filePath))
        //        {
        //            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
        //            StrokeCollection strokes = new StrokeCollection(fs);
        //            ip.Strokes = strokes;
        //            fs.Close();

        //            return strokes;
        //        }
        //        return null;
        //    }

        /// <summary>
        /// Returns a list of all folders stored in the folder.xml file
        /// </summary>
        /// <param name="path">The path to the user'd directory</param>
        /// <returns>A list of folder paths</returns>
        public static List<CodecInfo> GetFolderListing()
        {
            string folderFilePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Vtron\\HipcConfigurationTools" + @"\codec.xml";

            List<CodecInfo> folderList = new List<CodecInfo>();

            if (File.Exists(folderFilePath))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(folderFilePath);

                XmlNodeList xmlFolderList = doc.GetElementsByTagName("Codec");

                foreach (XmlNode node in xmlFolderList)
                {
                    XmlElement folderElement = (XmlElement)node;
                    CodecInfo codecInfo = new CodecInfo();
                    codecInfo.Index = Int32.Parse(folderElement.GetElementsByTagName("Index")[0].InnerText);
                    codecInfo.Ip = folderElement.GetElementsByTagName("Ip")[0].InnerText;
                    codecInfo.SignalType = folderElement.GetElementsByTagName("SignalType")[0].InnerText;
                    codecInfo.CodecType = folderElement.GetElementsByTagName("CodecType")[0].InnerText;
                    folderList.Add(codecInfo);
                }
                return folderList;
            }
            else
            {
                FileStream fs = File.Create(folderFilePath);
                fs.Close();
                folderList = new List<CodecInfo>();
            }
            return folderList;
        }

        /// <summary>
        /// Saves the current set of folders to disk
        /// </summary>
        /// <param name="folderList">A list of the folders this application is looking out for</param>
        public static void SaveFolderListing(List<CodecInfo> folderList)
        {
            string folderFilePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Vtron\\HipcConfigurationTools" + @"\codec.xml";

            XmlDocument xmlDoc = new XmlDocument();
            XmlElement mainElement = xmlDoc.CreateElement("HipcConfiguration");

            foreach (CodecInfo folderPath in folderList)
            {
                XmlElement folderElement = xmlDoc.CreateElement("Codec");

                XmlElement IndexElement = xmlDoc.CreateElement("Index");
                IndexElement.InnerText = folderPath.Index.ToString();
                folderElement.AppendChild(IndexElement);

                XmlElement IpElement = xmlDoc.CreateElement("Ip");
                IpElement.InnerText = folderPath.Ip;
                folderElement.AppendChild(IpElement);

                XmlElement SignaTypeElement = xmlDoc.CreateElement("SignalType");
                SignaTypeElement.InnerText = folderPath.SignalType;
                folderElement.AppendChild(SignaTypeElement);

                XmlElement CodecTypeElement = xmlDoc.CreateElement("CodecType");
                CodecTypeElement.InnerText = folderPath.CodecType;
                folderElement.AppendChild(CodecTypeElement);

                mainElement.AppendChild(folderElement);
            }

            xmlDoc.AppendChild(mainElement);
            xmlDoc.Save(folderFilePath);
        }

        //    /// <summary>
        //    /// Adds a folder for display
        //    /// </summary>
        //    /// <param name="path">The path of the folder to add</param>
        //    static public void AddFolder(string path)
        //    {
        //        ParentWindow.AddFolder(path);
        //    }

        //    /// <summary>
        //    /// Removes a folder from being displayed
        //    /// </summary>
        //    /// <param name="path">The path of the folder to remove</param>
        //    internal static void RemoveFolder(string path)
        //    {
        //        ParentWindow.RemoveFolder(path);
        //    }
    }
}
